
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>又拍云存储 - Mr.Song</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="又拍云存储
安装又拍云的控件pip install upyun

django接口1234567891011121314151617#又拍云import upyun#定义文件上传类class You,"> 
    <meta name="author" content="Mr.Song"> 
    
    <link rel="icon" href="/song_b/img/song.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/song_b/css/diaspora.css">

	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script>
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Mr.Song</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://gitee.com/song_shen_olympic/song_b.git"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">又拍云存储</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">又拍云存储</h1>
        <div class="stuff">
            <span>五月 03, 2020</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/song_b/tags/%E4%BA%91%E5%82%A8%E5%AD%98/" rel="tag">云储存</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="又拍云存储"><a href="#又拍云存储" class="headerlink" title="又拍云存储"></a>又拍云存储</h1><hr>
<h2 id="安装又拍云的控件"><a href="#安装又拍云的控件" class="headerlink" title="安装又拍云的控件"></a>安装又拍云的控件</h2><p>pip install upyun</p>
<hr>
<h2 id="django接口"><a href="#django接口" class="headerlink" title="django接口"></a>django接口</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#又拍云</span></span><br><span class="line"><span class="keyword">import</span> upyun</span><br><span class="line"><span class="comment">#定义文件上传类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">YouPai</span><span class="params">(View)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">post</span><span class="params">(self,request)</span>:</span></span><br><span class="line">        <span class="comment">#接受参数</span></span><br><span class="line">        img = request.FILES.get(<span class="string">'file'</span>)</span><br><span class="line">        <span class="comment"># 生成独特的文件名</span></span><br><span class="line">        filename = make_password(img.name) + str((int(round(time.time() * <span class="number">1000</span>)))) + <span class="string">".jpg"</span></span><br><span class="line">        <span class="comment"># 实例化对象</span></span><br><span class="line">        up = upyun.UpYun(<span class="string">'md-admin'</span>, username=<span class="string">'a2925087209'</span>, password=<span class="string">'by0sFyYQ62GfZSIqCPVRXJLHlierRJsT'</span>)</span><br><span class="line">        headers = &#123; <span class="string">'x-gmkerl-rotate'</span>: <span class="string">'50'</span> &#125;</span><br><span class="line">        <span class="comment"># 分块上传</span></span><br><span class="line">        <span class="keyword">for</span> chunk <span class="keyword">in</span> img.chunks():</span><br><span class="line">            res = up.put(<span class="string">'/%s'</span>%filename,chunk,checksum=<span class="literal">True</span>,headers=headers)</span><br><span class="line">        <span class="comment">#返回结果</span></span><br><span class="line">        <span class="keyword">return</span> HttpResponse(json.dumps(&#123;<span class="string">'filename'</span>:filename&#125;),content_type=<span class="string">'application/json'</span>)</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="vue接口"><a href="#vue接口" class="headerlink" title="vue接口"></a>vue接口</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;!-- &lt;&#x2F;img :src&#x3D;&quot;src&quot; &#x2F;&gt; --&gt;</span><br><span class="line">        又拍云存储：&lt;Avatar :src&#x3D;&quot;yp_url&quot; :width&#x3D;&#39;150&#39; fil&#x3D;&#39;fill&#39;&gt;&lt;&#x2F;Avatar&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;input type&#x3D;&quot;file&quot; @change&#x3D;&quot;upload_upyun&quot; &#x2F;&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;upload&quot;&gt;</span><br><span class="line">            拖拽上传</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    data () &#123;</span><br><span class="line">        return &#123;</span><br><span class="line">            yp_url :&#39;&#39;,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    mounted:function()&#123;</span><br><span class="line">    &#x2F;&#x2F;又拍云</span><br><span class="line">    &#x2F;&#x2F;注册推托拽容器</span><br><span class="line">    let upload &#x3D; document.querySelector(&#39;.upyun&#39;);</span><br><span class="line">    &#x2F;&#x2F;声明监听事件</span><br><span class="line">    &#x2F;&#x2F;点击</span><br><span class="line">    upload.addEventListener(&#39;dragenter&#39;, this.onDrag, false);</span><br><span class="line">    &#x2F;&#x2F;悬停</span><br><span class="line">    upload.addEventListener(&#39;dragover&#39;, this.onDrag, false);</span><br><span class="line">    &#x2F;&#x2F;释放</span><br><span class="line">    upload.addEventListener(&#39;drop&#39;, this.onDrop, false);</span><br><span class="line">    &#125;,</span><br><span class="line">    methods:&#123;</span><br><span class="line">        &#x2F;&#x2F;又拍云上传</span><br><span class="line">        &#x2F;&#x2F;监听鼠标</span><br><span class="line">        onDrag (e) &#123;</span><br><span class="line">        e.stopPropagation();</span><br><span class="line">        e.preventDefault();</span><br><span class="line">        &#125;,</span><br><span class="line">        onDrop (e) &#123;</span><br><span class="line">        e.stopPropagation();</span><br><span class="line">        e.preventDefault();</span><br><span class="line">        this.upyun(e.dataTransfer.files);</span><br><span class="line">        &#125;,</span><br><span class="line">        &#x2F;&#x2F;上传又拍云</span><br><span class="line">        upyun:function(files)&#123;</span><br><span class="line">            &#x2F;&#x2F;获取文件对象</span><br><span class="line">        &#x2F;&#x2F;   let file &#x3D; e.target.files[0];</span><br><span class="line">            let file &#x3D; files[0];</span><br><span class="line">            &#x2F;&#x2F;声明参数</span><br><span class="line">            let param &#x3D; new FormData();</span><br><span class="line">            param.append(&#39;file&#39;,file);</span><br><span class="line"></span><br><span class="line">            const config &#x3D; &#123;</span><br><span class="line">            headers: &#123; &#39;Content-Type&#39;: &#39;multipart&#x2F;form-data&#39; &#125;</span><br><span class="line">            &#125; </span><br><span class="line">            var a &#x3D; this</span><br><span class="line">            a.axios.post(&#39;http:&#x2F;&#x2F;localhost:8000&#x2F;youpai&#x2F;&#39;, param, config)&#x2F;&#x2F; 上传图片</span><br><span class="line">            .then(function(res) &#123;</span><br><span class="line">                console.log(res)	</span><br><span class="line">                a.yp_url &#x3D; &#39;http:&#x2F;&#x2F;md-admin.test.upcdn.net&#x2F;&#39;+res.data.filename</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">    &#125;,</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">.upload &#123;</span><br><span class="line">  margin: 100px auto;</span><br><span class="line">  width: 300px;</span><br><span class="line">  height: 150px;</span><br><span class="line">  border: 2px dashed #f00;</span><br><span class="line">  padding-top: 50px;</span><br><span class="line">  padding-left: 80px;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="又拍云文件操作"><a href="#又拍云文件操作" class="headerlink" title="又拍云文件操作"></a>又拍云文件操作</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> upyun</span><br><span class="line"><span class="comment">#实例化</span></span><br><span class="line">up = upyun.UpYun(<span class="string">"空间名"</span>,<span class="string">'操作员'</span>,<span class="string">'密码'</span>)</span><br><span class="line"><span class="comment">#读内存上传</span></span><br><span class="line">up.put(<span class="string">'自定义文件名'</span>,<span class="string">'文件内容'</span>)</span><br><span class="line"><span class="comment">#文件流操作（节省内存）</span></span><br><span class="line"><span class="keyword">with</span> open(<span class="string">'文件路径/文件名'</span>,<span class="string">'rb'</span>)<span class="keyword">as</span> f:</span><br><span class="line">    res = up.put(<span class="string">'自定义文件名'</span>,f,checksum=<span class="literal">True</span>)</span><br><span class="line"><span class="comment">#目录操作</span></span><br><span class="line">up.mkdir(<span class="string">'/文件夹名/'</span>)</span><br><span class="line"><span class="comment">#移动文件</span></span><br><span class="line">up.move(<span class="string">'/文件路径/文件名'</span>,<span class="string">'/新文件路径/文件名'</span>)</span><br><span class="line"><span class="comment">#复制文件</span></span><br><span class="line">up.copy(<span class="string">'/文件路径/文件名'</span>,<span class="string">'/新文件路径/文件名'</span>)</span><br><span class="line"><span class="comment">#断点续传</span></span><br><span class="line"><span class="keyword">with</span> open(<span class="string">'文件路径/文件名'</span>,<span class="string">'rb'</span>)<span class="keyword">as</span> f:</span><br><span class="line">    res = up.put(<span class="string">'自定义文件名'</span>,f,checksum=<span class="literal">True</span>，need_resume=<span class="literal">True</span>)</span><br><span class="line"><span class="comment">#下载</span></span><br><span class="line">res = up.get(<span class="string">'/文件路径/文件名'</span>)</span><br><span class="line"><span class="comment">#删除</span></span><br><span class="line">up.delete(<span class="string">'/文件路径/文件名'</span>)</span><br></pre></td></tr></table></figure>
            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="true">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='/song_b//img/002.mp3'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci=''
        data-cs=''
        data-r=''
        data-o=''
        data-a=''
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#又拍云存储"><span class="toc-number">1.</span> <span class="toc-text">又拍云存储</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#安装又拍云的控件"><span class="toc-number">1.1.</span> <span class="toc-text">安装又拍云的控件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#django接口"><span class="toc-number">1.2.</span> <span class="toc-text">django接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#vue接口"><span class="toc-number">1.3.</span> <span class="toc-text">vue接口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#又拍云文件操作"><span class="toc-number">1.4.</span> <span class="toc-text">又拍云文件操作</span></a></li></ol></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/song_b/js/plugin.js"></script>
<script src="/song_b/js/typed.js"></script>
<script src="/song_b/js/diaspora.js"></script>


<link rel="stylesheet" href="/song_b/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/song_b/photoswipe/default-skin/default-skin.css">


<script src="/song_b/photoswipe/photoswipe.min.js"></script>
<script src="/song_b/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
